$(function() {
	
	setTimeout(CartFn(),1000);
	
//	CartFn();

	$("#collction").on("tap", function() {
		//点击管理
		$(".cart_delbox").toggleClass('cart_delbox_show');
		$(".PD_content").toggleClass('cart_cont_show');
		$(".delbtn").on("tap", function() {
			//						点击删除按钮
			$.each($(".PD_content .icon-xuanzhong"), function(i, obj) {
				MuiAjaxFn("client/detailed/deletebycommodity", {
					'commodityNumber': $(this).attr("data-commodityNumber")
				}, function(data) {
					if(data.htmlState.status == 200) {
						mui.toast("删除商品成功");
						totalFn()
					} else {
						//									删除商品失败的处理
					}
				})
				$(".icon-xuanzhong").parent().parent().remove();
				if($('.p_libox').length == 0) {
					$("#allprice").html(0);
					nodata('您还没有添加商品哦.', '.PD_content')
				}
			})
		})
	})

	$("#Finishshop").on("tap", function() {
		if($(".icon-xuanzhong").length > 0) {
			GoToBuyListener();
			// OpenNewWindow("order_confirm.html","order_confirm.html",{},{},{});
		} else {
			mui.toast("您还没有选择商品");
		}
	})

})

/*立即购买*/
function GoToBuyListener() {

	var result = []; //购物清单

	$.each($(".PD_content  .icon-xuanzhong "), function(i, obj) {
		var commodityNumber = $(this).attr("data-commoditynumber");
		var count = ($(this).parent().parent().find('.linum').html()).substring(1);
		var price = ($(this).parent().parent().find('.price').html()).substring(1);
		var allprice = $(this).parent().parent().parent().next().find("#allprice").html()
		var allcount = $(".SCar-bot-total").attr("allcount")
		result.push({
			'commodityNumber': commodityNumber,
			'count': count,
			'price': price,
			'allprice': allprice,
			'allcount': allcount
		});
	})
	if(result.length > 0) { //当购物清单长度大于0，即可进入“确认订单”页面

		result["present"] = false;
		result = JSON.stringify(result); //转化为字符串
		console.log("转换成功！" + result);

		localStorage.setItem('goodsList', result);
		window.top.location.href = "order_confirm.html?t=" + new Date().getTime();;
	}

}

function CartliFN(idx) {
	//处理编辑状态与正常列表的显示
	$(".p_libox:eq(" + idx + ") .p_detailed").toggle();
	$(".p_libox:eq(" + idx + ") .menusover").toggle();
}

function totalFn() {
	//处理总价
	var price = 0; //单价
	var num = 0; //数量//
	var total = 0; //总价
	var countnum = 0;
	$.each($(".PD_content .icon-xuanzhong"), function(i, obj) {
			var $obj = $(this).parent().parent();
			total += GetNumFn($obj.find('.linum').html(), $obj.find('.price').html())
			countnum += parseInt(($obj.find('.linum').html()).substring(1))

		})
		//	console.log(total)
		//	console.log(countnum)
	$(".SCar-bot-total").attr("allcount", countnum)
	$("#allprice").html(total / 100);

}

function CartFn() {
	//购物车渲染

	AjaxLGettFn('client/detailed/selectbyuserId?adcode=100000&page=1&pagesize=100', function(data) {
		var str = '';
		if(data.pageInfo.size != 0) {
			//					查询购物车列表
			$.each(data.pageInfo.list, function(i, obj) {
				if(obj.commodityState == 200) {

					ResultObj = obj.dataResultObj;
					presentPrice = (ResultObj.dataResultObj.presentPrice / 100).toFixed(2);
					str += '<div class="p_libox" data-commodityNumber="' + obj.commodityNumber + '" >' +
						'<div class="check_box"><span class=" iconfont icon-quan" data-commodityNumber="' + obj.commodityNumber + '"></span></div>' +
						'<div class="menusover"  style="display: none;" data-commodityNumber="' + obj.commodityNumber + '"><span>完成</span></div>' +
						'<div class="p_right">' +
						'<div class="p_top">' +
						'<div> <div class="ps_img" style="background-image: url(' + IMGUrl + '' + ResultObj.mainPhoto + ') "></div></div>' +
						'<div class="p_detailed" >' +
						'	<div class="d_cont">' +
						'		<p class="phide2">' + ResultObj.title + '</p>' +
						//					'		<i>规格:</i>' +
						'		<span>自主配送</span>' +
						'	</div>' +
						'	<div class="d_price"><span class="price">￥' + presentPrice + '</span>' +
						'<span class="linum">X' + obj.needNumber + '</span></div>' +
						'</div>' +
						'<div class="p_detailed" style="display: none;" >' +
						'	<div class="numboxtop">' +
						'		<div class="app-numbox"  data-numbox-min="1" data-numbox-max="100">' +
						'		<span class="iconfont icon-jian"></span><input value="' + obj.needNumber + '" type="number" class="app-num"> <span class="iconfont icon-jia"></span>	' +
						'		</div>' +
						//					'		<div class="proect">样式选择 <span class="iconfont icon-right2"></span></div>' +
						'	</div></div></div>' +
						'<div class="p_bot"><span class="iconfont icon-bianji checkedit_menu"></span></div></div></div>';
					//					库存:' + obj.dataResultObj.merchantNumber + '张</p>
				}

			})

			//		window.top.location.href=\'ProductDetail.html?commodityNumber=' + obj2.commodityNumber + '\'

			$(".PD_content").html(str);

			$(".ps_img , .d_cont , .d_price").on("tap", function() {
				var cN = $(this).parent().parent().parent().parent().attr("data-commodityNumber");
				window.top.location.href = 'ProductDetail.html?commodityNumber=' + cN;
			})
			if($(".PD_content div").length <= 0) {
				nodata('您还没有添加商品哦.', '.PD_content');
			}
		} else {
			nodata('您还没有添加商品哦.', '.PD_content');
		};
		addminuFn();

		$(".checkedit_menu").on('tap', function() {
			//点击编辑图标.进入单项修改数量样式
			CartliFN($(".checkedit_menu").index($(this)))

		})

		$(".menusover").on('tap', function() {
			//点击完成
			CartliFN($(".menusover").index($(this)));
			$(this).parent().find('.linum').html("X" + $(this).parent().find('.app-num').val())
			MuiAjaxFn("client/detailed/updatebyuserid", {
				'commodityNumber': $(this).attr("data-commodityNumber"),
				'needNumber': $(this).parent().find('.app-num').val()
			}, function(data) {
				//修改购物车
				if(data.htmlState.status == 200) {
					mui.toast('修改商品成功');
					totalFn();
				} else {
					//								修改商品不成功时.应该进行一定的处理
				}
				console.log(data)
			})
		})
		$(".icon-quan").on('tap', function() {
			//点击原点
			$(this).toggleClass("icon-xuanzhong");
			totalFn()
		})

		$("#allcheck").on('tap', function() {
			//点击全选
			$(".icon-quan").attr('class', $("#allcheck .icon-quan").attr('class'));
			totalFn();
		})

		$(".app-numbox input").change(function() {
			//点击加减修改合计处的价格
			totalFn();
		})

	})
}

//		icon-xuanzhong